package cn.tq365.type.mybatis;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import cn.tq365.cm.ConstMap;

import com.soso.tg.api.advertise.accountservice.v1.AdvertiserStatusEnum;

/**
 * 
 * @author lili
 * 
 */
@MappedTypes(value = { AdvertiserStatusEnum.class })
@MappedJdbcTypes(JdbcType.INTEGER)
public class SoSoAccountSituationTypeHandler extends
		BaseTypeHandler<AdvertiserStatusEnum> {
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i,
			AdvertiserStatusEnum parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setInt(i, ConstMap.situationMap.get(parameter.value()));
	}

	@Override
	public AdvertiserStatusEnum getNullableResult(ResultSet rs,
			String columnName) throws SQLException {
		int rint = rs.getInt(columnName);
		Set<Entry<Object, Integer>> entrySet = ConstMap.situationMap.entrySet();
		for (Entry<Object, Integer> entry : entrySet) {
			if (entry.getValue() == rint) {
				return AdvertiserStatusEnum.fromValue((String) entry.getKey());
			}
		}
		return null;
	}

	@Override
	public AdvertiserStatusEnum getNullableResult(CallableStatement cs,
			int columnIndex) throws SQLException {
		int rint = cs.getInt(columnIndex);
		Set<Entry<Object, Integer>> entrySet = ConstMap.situationMap.entrySet();
		for (Entry<Object, Integer> entry : entrySet) {
			if (entry.getValue() == rint) {
				return AdvertiserStatusEnum.fromValue((String) entry.getKey());
			}
		}
		return null;
	}

	@Override
	public AdvertiserStatusEnum getNullableResult(ResultSet rs, int columnIndex)
			throws SQLException {
		int rint = rs.getInt(columnIndex);
		Set<Entry<Object, Integer>> entrySet = ConstMap.situationMap.entrySet();
		for (Entry<Object, Integer> entry : entrySet) {
			if (entry.getValue() == rint) {
				return AdvertiserStatusEnum.fromValue((String) entry.getKey());
			}
		}
		return null;
	}
}
